
package com.studiofortress.sf.util.structure.loading;

import com.studiofortress.sf.graphics.GLTexture;

/**
 * A LoadAction who's action is to load a texture. The texture to load is
 * specified by the given texturePath. The texture will be pre-loaded into the
 * current GLTexture manager when this is called.
 *
 * @author Joseph Lenton
 */
public class LoadTextureAction implements LoadAction
{
    /**
     * Typically when you create a LoadTextureAction you actually want to create
     * multiple ones. This will create and return an entire array of
     * LoadTextureActions from the paths in the given paths array.
     * @param paths The paths to the textures to pre-load.
     * @return An array containing LoadTextureActions for each of the paths given.
     */
    public static LoadTextureAction[] newBatch(String ... paths)
    {
        if ( paths == null ) {
            throw new IllegalArgumentException("The given paths cannot be null.");
        }

        final LoadTextureAction[] actions = new LoadTextureAction[paths.length];

        for ( int i = 0; i < actions.length; i++ ) {
            actions[i] = new LoadTextureAction( paths[i] );
        }

        return actions;
    }

    private final String texturePath;

    /**
     * A new LoadTextureAction that will load the given texture found at the
     * given path.
     * @param texturePath The path to the texture to load, cannot be null or an empty string.
     */
    public LoadTextureAction(String texturePath)
    {
        if (texturePath == null) {
            throw new IllegalArgumentException("The given texturePath cannot be null.");
        }
        if (texturePath.length() == 0) {
            throw new IllegalArgumentException("The given texturePath cannot be an empty string.");
        }

        this.texturePath = texturePath;
    }

    /**
     * @return The path to the texture that this will be loading.
     */
    public String getTexturePath()
    {
        return texturePath;
    }

    /**
     * Finds and loads the texture at the end of the texturePath.
     */
    public void action()
    {
        GLTexture.getTextureManager().load( getTexturePath() );
    }
}
